{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a4d5d1bc-df65-47ae-8e2a-d1fe6218fb3e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-01T12:23:59.915289Z",
     "iopub.status.busy": "2022-06-01T12:23:59.914744Z",
     "iopub.status.idle": "2022-06-01T12:24:01.779535Z",
     "shell.execute_reply": "2022-06-01T12:24:01.778894Z",
     "shell.execute_reply.started": "2022-06-01T12:23:59.915212Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 1.73 s, sys: 107 ms, total: 1.84 s\n",
      "Wall time: 1.85 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "from torch.utils.data import Dataset\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import os\n",
    "\n",
    "import torchvision.transforms as transforms\n",
    "from torchvision.utils import save_image\n",
    "from torch.utils.data import DataLoader\n",
    "import torch.utils.data as Data\n",
    "from torchvision import datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "029bc639-dace-4661-875b-ec0605f5f8e2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-01T12:24:01.781157Z",
     "iopub.status.busy": "2022-06-01T12:24:01.780876Z",
     "iopub.status.idle": "2022-06-01T12:24:01.787959Z",
     "shell.execute_reply": "2022-06-01T12:24:01.787291Z",
     "shell.execute_reply.started": "2022-06-01T12:24:01.781133Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GPU:False\n"
     ]
    }
   ],
   "source": [
    "class Parse:\n",
    "    def __init__(self):  #定义超参数\n",
    "        self.channels = 1\n",
    "        self.img_size = 28\n",
    "        self.latent_dim = 100\n",
    "        self.epochs = 200\n",
    "        self.batch_size = 64\n",
    "        self.lr = 0.00005\n",
    "        self.cpu_nums = 8\n",
    "        self.classes = 10\n",
    "        self.want_cuda = \"cuda:0\"\n",
    "        self.clip_value = 0.01\n",
    "        self.n_critic = 5  # 训练5次D，训练1次G\n",
    "        \n",
    "        \n",
    "\n",
    "        # 下面是衍生的\n",
    "        self.img_shape = (self.channels, self.img_size, self.img_size)\n",
    "        self.cuda = torch.cuda.is_available()\n",
    "        self.device = self.want_cuda if self.cuda else 'cpu'\n",
    "        self.big_cuda = True  # 能不能把数据集全部放入GPU\n",
    "        print(f\"GPU:{self.cuda}\")\n",
    "opt = Parse()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9a9197d7-5606-4b26-804b-c4e64a5f1354",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-01T12:24:01.789272Z",
     "iopub.status.busy": "2022-06-01T12:24:01.789062Z",
     "iopub.status.idle": "2022-06-01T12:24:01.795037Z",
     "shell.execute_reply": "2022-06-01T12:24:01.794492Z",
     "shell.execute_reply.started": "2022-06-01T12:24:01.789249Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "class Tools:\n",
    "    # 装一些杂七杂八的工具\n",
    "    def block(in_feat, out_feat, normalize=True):\n",
    "        layers = [nn.Linear(in_feat, out_feat),nn.LeakyReLU(0.2, inplace=True)]\n",
    "        if normalize:\n",
    "            layers.insert(1, nn.BatchNorm1d(out_feat, 0.8))\n",
    "        return layers\n",
    "    \n",
    "    \n",
    "    def sample_image(n_row, batches_done, generator):\n",
    "\n",
    "        z = torch.randn(n_row ** 2, opt.latent_dim, device=opt.device)\n",
    "        gen_imgs = generator(z)\n",
    "        save_image(gen_imgs.data, \"images/%d.png\" % batches_done, nrow=n_row, normalize=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c8d46030-6bfa-40f4-a98e-9208c741d7b9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-01T12:24:01.796325Z",
     "iopub.status.busy": "2022-06-01T12:24:01.795929Z",
     "iopub.status.idle": "2022-06-01T12:24:01.801397Z",
     "shell.execute_reply": "2022-06-01T12:24:01.800918Z",
     "shell.execute_reply.started": "2022-06-01T12:24:01.796303Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "class Generator(nn.Module):\n",
    "    def __init__(self):\n",
    "        \n",
    "        super().__init__()\n",
    "\n",
    "        \n",
    "        self.model = nn.Sequential(\n",
    "            *Tools.block(opt.latent_dim, 128, normalize=False),\n",
    "            *Tools.block(128, 256),\n",
    "            *Tools.block(256, 512),\n",
    "            *Tools.block(512, 1024),\n",
    "            nn.Linear(1024, int(np.prod(opt.img_shape))),\n",
    "            nn.Tanh())\n",
    "        \n",
    "        self.optimizer = torch.optim.RMSprop(self.parameters(), lr=opt.lr)\n",
    "        \n",
    "    def forward(self, noise):\n",
    "        img = self.model(noise)\n",
    "        img = img.view(img.size(0), *opt.img_shape)\n",
    "        return img\n",
    "        \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9724604b-1ac2-4ceb-a049-bb826b26aec3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-01T12:24:01.802980Z",
     "iopub.status.busy": "2022-06-01T12:24:01.802538Z",
     "iopub.status.idle": "2022-06-01T12:24:01.808067Z",
     "shell.execute_reply": "2022-06-01T12:24:01.807578Z",
     "shell.execute_reply.started": "2022-06-01T12:24:01.802932Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "class Discriminator(nn.Module):\n",
    "    def __init__(self):\n",
    "        \n",
    "        super().__init__()\n",
    "        \n",
    "\n",
    "        self.model = nn.Sequential(\n",
    "            nn.Linear(int(np.prod(opt.img_shape)), 512),\n",
    "            nn.LeakyReLU(0.2, inplace=True),\n",
    "            nn.Linear(512, 256),\n",
    "            nn.LeakyReLU(0.2, inplace=True),\n",
    "            nn.Linear(256, 1),\n",
    "        )\n",
    "        self.optimizer = torch.optim.RMSprop(self.parameters(), lr=opt.lr) \n",
    "        \n",
    "\n",
    "    def forward(self, imgs):\n",
    "        a = imgs.view(imgs.shape[0], -1)\n",
    "        validity = self.model(a)\n",
    "        return validity\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d3f43013-2776-4506-8ada-ea7b2674dc64",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-01T12:24:01.809502Z",
     "iopub.status.busy": "2022-06-01T12:24:01.809149Z",
     "iopub.status.idle": "2022-06-01T12:24:01.822517Z",
     "shell.execute_reply": "2022-06-01T12:24:01.821957Z",
     "shell.execute_reply.started": "2022-06-01T12:24:01.809475Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "class Dev:\n",
    "    def __init__(self):\n",
    "        self.g = Generator()\n",
    "        self.d = Discriminator()\n",
    "        # self.loss = nn.MSELoss()\n",
    "        \n",
    "        self.models = (self.g, self.d)\n",
    "        \n",
    "        self.losses1 = []\n",
    "        self.losses2 = []\n",
    "        \n",
    "    def load_data(self):\n",
    "\n",
    "        def pretreat():\n",
    "            train = pd.read_csv('../../data/mnist/train.csv', header=None).values\n",
    "            # train, train_label = torch.tensor(train[...,1:]/255, dtype=torch.float), torch.tensor(train[...,0], dtype=torch.long)\n",
    "            train, train_label = torch.tensor((train[...,1:]/255-0.5)/0.5, dtype=torch.float), torch.tensor(train[...,0], dtype=torch.long)\n",
    "            return [train, train_label]\n",
    "        self.data = pretreat()\n",
    "        self.dataloader = torch.utils.data.DataLoader(\n",
    "            Data.TensorDataset(*self.data),\n",
    "            batch_size=opt.batch_size,\n",
    "            shuffle=True,\n",
    "            num_workers=opt.cpu_nums,\n",
    "            drop_last=True)\n",
    "        \n",
    "    def to_cuda(self):\n",
    "        opt.cuda = True\n",
    "        opt.device = opt.want_cuda\n",
    "        for i in self.models:\n",
    "            i.cuda()\n",
    "        if opt.big_cuda:\n",
    "            self.dataloader = torch.utils.data.DataLoader(\n",
    "                Data.TensorDataset(*[i.to(opt.device) for i in self.data]),\n",
    "                batch_size=opt.batch_size,\n",
    "                shuffle=True,\n",
    "                # num_workers=opt.cpu_nums,\n",
    "                drop_last=True)\n",
    "       \n",
    "    def to_cpu(self):\n",
    "        opt.cuda = False\n",
    "        opt.device = 'cpu'\n",
    "        for i in self.models:\n",
    "            i.cpu()\n",
    "        self.dataloader = torch.utils.data.DataLoader(\n",
    "            Data.TensorDataset(*self.data),\n",
    "            batch_size=opt.batch_size,\n",
    "            shuffle=True,\n",
    "            num_workers=opt.cpu_nums,\n",
    "            drop_last=True)\n",
    "        \n",
    "        \n",
    "    def train(self):\n",
    "        \n",
    "        losses1 = []\n",
    "        \n",
    "        for i, (imgs, labels) in enumerate(self.dataloader):\n",
    "            \n",
    "            z = torch.randn(imgs.shape[0], opt.latent_dim, device=opt.device)\n",
    "            fake_imgs = self.g(z)\n",
    "            fake_imgs2 = fake_imgs.detach() # 一个给d用，一个给g用\n",
    "        \n",
    "            if i % opt.n_critic == 0:\n",
    "                self.g.optimizer.zero_grad()\n",
    "                loss_G = - torch.mean(self.d(fake_imgs))\n",
    "                loss_G.backward()\n",
    "                self.g.optimizer.step()\n",
    "            \n",
    "            self.d.optimizer.zero_grad()\n",
    "            d_fake_loss = -torch.mean(self.d(imgs)) + torch.mean(self.d(fake_imgs2))\n",
    "            d_fake_loss.backward()\n",
    "            self.d.optimizer.step()\n",
    "            \n",
    "            for p in self.d.parameters():  # 新增\n",
    "                p.data.clamp_(-opt.clip_value, opt.clip_value)\n",
    "            \n",
    "            \n",
    "            losses1.append(d_fake_loss.item())\n",
    "        self.losses1.append(sum(losses1)/len(losses1))\n",
    "        print(self.losses1[-1], end=' ')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "550e8797-867c-4db5-8628-9ca63d19d811",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-01T12:24:01.823736Z",
     "iopub.status.busy": "2022-06-01T12:24:01.823339Z",
     "iopub.status.idle": "2022-06-01T12:24:05.304079Z",
     "shell.execute_reply": "2022-06-01T12:24:05.303453Z",
     "shell.execute_reply.started": "2022-06-01T12:24:01.823716Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "dev = Dev()\n",
    "dev.load_data()\n",
    "# dev.to_cuda()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c6a63a06-bc71-4791-99b6-d888511378f3",
   "metadata": {
    "collapsed": true,
    "execution": {
     "iopub.execute_input": "2022-06-01T12:24:05.305970Z",
     "iopub.status.busy": "2022-06-01T12:24:05.305492Z",
     "iopub.status.idle": "2022-06-01T12:58:41.850085Z",
     "shell.execute_reply": "2022-06-01T12:58:41.849445Z",
     "shell.execute_reply.started": "2022-06-01T12:24:05.305944Z"
    },
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 1 -4.1993005533912395 耗时9.99186086654663s\n",
      "epoch 2 -0.3357766150410427 耗时10.509236335754395s\n",
      "epoch 3 -0.05754495252285594 耗时10.2750825881958s\n",
      "epoch 4 -0.0799061861753925 耗时10.213683128356934s\n",
      "epoch 5 -0.16106639391958333 耗时10.370117664337158s\n",
      "epoch 6 -0.278978749044168 耗时10.373330116271973s\n",
      "epoch 7 -0.2893401367814462 耗时10.252791404724121s\n",
      "epoch 8 -0.30700531662337427 耗时10.394692659378052s\n",
      "epoch 9 -0.3464632268394706 耗时10.303249835968018s\n",
      "epoch 10 -0.3015854391751447 耗时10.310430526733398s\n",
      "epoch 11 -0.29998560928865836 耗时10.362503051757812s\n",
      "epoch 12 -0.3577395351235869 耗时10.1182541847229s\n",
      "epoch 13 -0.3149308247398287 耗时10.148029804229736s\n",
      "epoch 14 -0.3610880023769025 耗时10.233707427978516s\n",
      "epoch 15 -0.4191166429662145 耗时10.349618673324585s\n",
      "epoch 16 -0.4231017524557409 耗时10.22414255142212s\n",
      "epoch 17 -0.433363535360949 耗时10.276773691177368s\n",
      "epoch 18 -0.4071780905016204 耗时10.282719850540161s\n",
      "epoch 19 -0.3440177377031096 耗时10.089312553405762s\n",
      "epoch 20 -0.4037454235133901 耗时10.408684492111206s\n",
      "epoch 21 -0.3766841071646903 耗时10.407004356384277s\n",
      "epoch 22 -0.37670198423249235 耗时10.450978517532349s\n",
      "epoch 23 -0.3844578111566118 耗时10.452470302581787s\n",
      "epoch 24 -0.3566646350740496 耗时10.503591060638428s\n",
      "epoch 25 -0.32019605148054875 耗时10.450268030166626s\n",
      "epoch 26 -0.32932895084837077 耗时10.499329328536987s\n",
      "epoch 27 -0.3236599194965342 耗时10.543151140213013s\n",
      "epoch 28 -0.308894675467541 耗时10.44971513748169s\n",
      "epoch 29 -0.2984148863158333 耗时10.455252170562744s\n",
      "epoch 30 -0.27962060213852336 耗时10.567489862442017s\n",
      "epoch 31 -0.2857361797843697 耗时10.486141681671143s\n",
      "epoch 32 -0.263280103657009 耗时10.398773908615112s\n",
      "epoch 33 -0.27619418161528597 耗时10.378950834274292s\n",
      "epoch 34 -0.25590660248901953 耗时10.46262502670288s\n",
      "epoch 35 -0.26914498594807207 耗时10.614990472793579s\n",
      "epoch 36 -0.26628384149952405 耗时10.58663296699524s\n",
      "epoch 37 -0.2600295427896424 耗时10.326527118682861s\n",
      "epoch 38 -0.24992712915516205 耗时10.317307949066162s\n",
      "epoch 39 -0.23517419485297758 耗时10.334104299545288s\n",
      "epoch 40 -0.22722396041438572 耗时10.278181314468384s\n",
      "epoch 41 -0.21689096051829856 耗时10.25691556930542s\n",
      "epoch 42 -0.2174112465999933 耗时10.182554960250854s\n",
      "epoch 43 -0.20962349175134615 耗时10.329192161560059s\n",
      "epoch 44 -0.22359860897573106 耗时10.22185492515564s\n",
      "epoch 45 -0.21776158501778747 耗时10.253149509429932s\n",
      "epoch 46 -0.21649333939608287 耗时10.329219818115234s\n",
      "epoch 47 -0.20340007320921094 耗时10.141791343688965s\n",
      "epoch 48 -0.19935766966136823 耗时10.258306980133057s\n",
      "epoch 49 -0.2088691134844671 耗时10.206745386123657s\n",
      "epoch 50 -0.2097510724082955 耗时10.32893705368042s\n",
      "epoch 51 -0.19597720870849544 耗时10.275310277938843s\n",
      "epoch 52 -0.19245410143629338 耗时10.11199951171875s\n",
      "epoch 53 -0.20283944197118345 耗时10.22833800315857s\n",
      "epoch 54 -0.1930669865460635 耗时10.486748456954956s\n",
      "epoch 55 -0.1855127857232679 耗时10.175397396087646s\n",
      "epoch 56 -0.18397091216186068 耗时10.321443557739258s\n",
      "epoch 57 -0.18521883342665568 耗时10.321317195892334s\n",
      "epoch 58 -0.18612953234889337 耗时10.22255277633667s\n",
      "epoch 59 -0.18269598929324807 耗时10.156226634979248s\n",
      "epoch 60 -0.17505673385099005 耗时10.407591581344604s\n",
      "epoch 61 -0.16663443393361224 耗时10.362865924835205s\n",
      "epoch 62 -0.16807487934605161 耗时10.085665702819824s\n",
      "epoch 63 -0.16375185662170866 耗时10.303670167922974s\n",
      "epoch 64 -0.15905416915103746 耗时10.278392553329468s\n",
      "epoch 65 -0.16318982922280229 耗时10.10895586013794s\n",
      "epoch 66 -0.16119620716050187 耗时10.26956057548523s\n",
      "epoch 67 -0.15957216264852975 耗时10.346261262893677s\n",
      "epoch 68 -0.16248856919043347 耗时10.458220481872559s\n",
      "epoch 69 -0.1538735807514496 耗时10.498780012130737s\n",
      "epoch 70 -0.15456590072322426 耗时10.381874561309814s\n",
      "epoch 71 -0.15271017060335826 耗时10.309048175811768s\n",
      "epoch 72 -0.15147270158871515 耗时10.480674505233765s\n",
      "epoch 73 -0.15419668576251735 耗时10.422523260116577s\n",
      "epoch 74 -0.13933516057603768 耗时10.233668804168701s\n",
      "epoch 75 -0.14662582948088265 耗时10.321757555007935s\n",
      "epoch 76 -0.14911219760727856 耗时10.369645595550537s\n",
      "epoch 77 -0.15186696780275 耗时10.404525518417358s\n",
      "epoch 78 -0.14989597006057853 耗时10.436826467514038s\n",
      "epoch 79 -0.15160110920445005 耗时10.273491144180298s\n",
      "epoch 80 -0.1380602074280111 耗时10.329967260360718s\n",
      "epoch 81 -0.13685016749126552 耗时10.376908302307129s\n",
      "epoch 82 -0.14100734808401721 耗时10.19375991821289s\n",
      "epoch 83 -0.1389615940309767 耗时10.215303182601929s\n",
      "epoch 84 -0.13645521095748137 耗时10.390591621398926s\n",
      "epoch 85 -0.13546655984672945 耗时10.259422540664673s\n",
      "epoch 86 -0.13236129805525187 耗时10.298444509506226s\n",
      "epoch 87 -0.12891329238101792 耗时10.187671184539795s\n",
      "epoch 88 -0.12520322194094338 耗时10.17815089225769s\n",
      "epoch 89 -0.12489874436735725 耗时10.321399927139282s\n",
      "epoch 90 -0.12345986305204247 耗时10.332603216171265s\n",
      "epoch 91 -0.12159203363457255 耗时10.350526571273804s\n",
      "epoch 92 -0.12169896576676832 耗时10.199480295181274s\n",
      "epoch 93 -0.12126793830855297 耗时10.18655514717102s\n",
      "epoch 94 -0.11666240508935495 耗时10.204365253448486s\n",
      "epoch 95 -0.11694828067034514 耗时10.368680715560913s\n",
      "epoch 96 -0.12067509588080766 耗时10.269269943237305s\n",
      "epoch 97 -0.11975620217867823 耗时10.549117088317871s\n",
      "epoch 98 -0.11970223968286016 耗时10.397167444229126s\n",
      "epoch 99 -0.11783933181518425 耗时10.25857138633728s\n",
      "epoch 100 -0.11888429881923354 耗时10.193327903747559s\n",
      "epoch 101 -0.12027007716696952 耗时10.265767574310303s\n",
      "epoch 102 -0.1134893097770634 耗时10.343876600265503s\n",
      "epoch 103 -0.11640969768023414 耗时10.443817138671875s\n",
      "epoch 104 -0.11530654305709465 耗时10.206629276275635s\n",
      "epoch 105 -0.11319995893358294 耗时10.248281955718994s\n",
      "epoch 106 -0.1124285109651254 耗时10.192741394042969s\n",
      "epoch 107 -0.11206942459562418 耗时10.37768030166626s\n",
      "epoch 108 -0.11171466665563105 耗时10.240676164627075s\n",
      "epoch 109 -0.11419510459696343 耗时10.250999212265015s\n",
      "epoch 110 -0.11292516841705225 耗时10.399772882461548s\n",
      "epoch 111 -0.11187557731392289 耗时10.342035055160522s\n",
      "epoch 112 -0.11121410621268518 耗时10.409325361251831s\n",
      "epoch 113 -0.11190985310166725 耗时10.252404928207397s\n",
      "epoch 114 -0.11085418219754638 耗时10.519218683242798s\n",
      "epoch 115 -0.1101421154677677 耗时10.336241245269775s\n",
      "epoch 116 -0.10784173139004213 耗时10.423799753189087s\n",
      "epoch 117 -0.11175521740664031 耗时10.213003635406494s\n",
      "epoch 118 -0.10763762702046234 耗时10.281473398208618s\n",
      "epoch 119 -0.1042222717146736 耗时10.3518545627594s\n",
      "epoch 120 -0.10297304945287226 耗时10.24441933631897s\n",
      "epoch 121 -0.10281919618808091 耗时10.462899446487427s\n",
      "epoch 122 -0.10289045866169313 耗时10.315487384796143s\n",
      "epoch 123 -0.10208664632530579 耗时10.313183307647705s\n",
      "epoch 124 -0.09808540929470652 耗时10.360805988311768s\n",
      "epoch 125 -0.09773068137967854 耗时10.23737359046936s\n",
      "epoch 126 -0.0980242782112374 耗时10.34872841835022s\n",
      "epoch 127 -0.09722922451340911 耗时10.180363893508911s\n",
      "epoch 128 -0.09977939721741569 耗时10.400158405303955s\n",
      "epoch 129 -0.09626650746629485 耗时10.24109697341919s\n",
      "epoch 130 -0.09674802890073274 耗时10.39822769165039s\n",
      "epoch 131 -0.0978629314276045 耗时10.252642154693604s\n",
      "epoch 132 -0.09729438275893953 耗时10.19302487373352s\n",
      "epoch 133 -0.09529083973570084 耗时10.30556607246399s\n",
      "epoch 134 -0.0964699320757529 耗时10.480947017669678s\n",
      "epoch 135 -0.09335259185147769 耗时10.44025468826294s\n",
      "epoch 136 -0.09134496848636656 耗时10.470338582992554s\n",
      "epoch 137 -0.09198528903525566 耗时10.502343893051147s\n",
      "epoch 138 -0.08800905949278091 耗时10.61932921409607s\n",
      "epoch 139 -0.08876257797697183 耗时10.42583703994751s\n",
      "epoch 140 -0.08912010864616077 耗时10.398473024368286s\n",
      "epoch 141 -0.08936819042950837 耗时10.352733850479126s\n",
      "epoch 142 -0.09109234250088002 耗时10.618963479995728s\n",
      "epoch 143 -0.09098252440974705 耗时10.372161388397217s\n",
      "epoch 144 -0.09040850814404075 耗时10.428846836090088s\n",
      "epoch 145 -0.08846051843087473 耗时10.452907800674438s\n",
      "epoch 146 -0.08708109850562878 耗时10.53358769416809s\n",
      "epoch 147 -0.08796154294985972 耗时10.308406591415405s\n",
      "epoch 148 -0.08790931121516762 耗时10.448955535888672s\n",
      "epoch 149 -0.08496614123357653 耗时10.454264163970947s\n",
      "epoch 150 -0.08792424583638618 耗时10.32975435256958s\n",
      "epoch 151 -0.08455217100894566 耗时10.551509857177734s\n",
      "epoch 152 -0.0844155859158413 耗时10.325245380401611s\n",
      "epoch 153 -0.08254489954660389 耗时10.466932535171509s\n",
      "epoch 154 -0.08235956917704743 耗时10.618257761001587s\n",
      "epoch 155 -0.08366297759521224 耗时10.623985052108765s\n",
      "epoch 156 -0.08222141769677878 耗时10.326100587844849s\n",
      "epoch 157 -0.08231672431514256 耗时10.351606607437134s\n",
      "epoch 158 -0.08335914657617201 耗时10.263722658157349s\n",
      "epoch 159 -0.08152148868638144 耗时10.44900894165039s\n",
      "epoch 160 -0.08146134130219385 耗时10.31258773803711s\n",
      "epoch 161 -0.08135820860033229 耗时10.507327795028687s\n",
      "epoch 162 -0.07825691824661629 耗时10.309547662734985s\n",
      "epoch 163 -0.07932276150714626 耗时10.32505989074707s\n",
      "epoch 164 -0.07786148538457165 耗时10.412055492401123s\n",
      "epoch 165 -0.07645766050609479 耗时10.439682006835938s\n",
      "epoch 166 -0.07781154343514458 耗时10.235353469848633s\n",
      "epoch 167 -0.07746479783521264 耗时10.300793170928955s\n",
      "epoch 168 -0.07621537812109817 耗时10.35756802558899s\n",
      "epoch 169 -0.07527047491124499 耗时10.304530143737793s\n",
      "epoch 170 -0.07545718108577179 耗时10.39425253868103s\n",
      "epoch 171 -0.07383576961057291 耗时10.332503080368042s\n",
      "epoch 172 -0.07377515862056769 耗时10.305019855499268s\n",
      "epoch 173 -0.07157577572662777 耗时10.213566780090332s\n",
      "epoch 174 -0.07094944350365769 耗时10.125214099884033s\n",
      "epoch 175 -0.07216402904201089 耗时10.225312232971191s\n",
      "epoch 176 -0.07087565638848532 耗时10.37386178970337s\n",
      "epoch 177 -0.07104763048308382 耗时10.385916471481323s\n",
      "epoch 178 -0.07097610490935335 耗时10.259000301361084s\n",
      "epoch 179 -0.07044932452947887 耗时10.086969137191772s\n",
      "epoch 180 -0.07028681847685302 耗时10.437224626541138s\n",
      "epoch 181 -0.06838668703142327 耗时10.557586431503296s\n",
      "epoch 182 -0.0696686866316495 耗时10.387582063674927s\n",
      "epoch 183 -0.06773983439869917 耗时10.62420916557312s\n",
      "epoch 184 -0.06819981686460806 耗时10.33381462097168s\n",
      "epoch 185 -0.06674212770757197 耗时10.495608568191528s\n",
      "epoch 186 -0.06730465471680926 耗时10.49641466140747s\n",
      "epoch 187 -0.061382460110724416 耗时10.509025812149048s\n",
      "epoch 188 -0.05580228535317306 耗时10.62278962135315s\n",
      "epoch 189 -0.06495672547066605 耗时10.448892593383789s\n",
      "epoch 190 -0.0639888146134807 耗时11.04155945777893s\n",
      "epoch 191 -0.06682500653485733 耗时11.671727895736694s\n",
      "epoch 192 -0.06651161409111389 耗时11.716697216033936s\n",
      "epoch 193 -0.06842727696755653 耗时11.635855674743652s\n",
      "epoch 194 -0.0700351903125596 耗时11.697685241699219s\n",
      "epoch 195 -0.07229034903209772 耗时11.148549556732178s\n",
      "epoch 196 -0.07136773019870038 耗时10.469152688980103s\n",
      "epoch 197 -0.07133229081633251 耗时10.465761661529541s\n",
      "epoch 198 -0.0703061742741881 耗时10.56263542175293s\n",
      "epoch 199 -0.071541388490284 耗时10.543035984039307s\n",
      "epoch 200 -0.069531428775767 耗时10.406408548355103s\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "time0 = time.time()\n",
    "for epoch in range(200):\n",
    "    print(f'epoch {epoch+1}', end=' ')\n",
    "    dev.train()\n",
    "    time0, time_spend = time.time(), time.time()-time0\n",
    "    print(f'耗时{time_spend}s')\n",
    "    if not epoch % 1:\n",
    "        Tools.sample_image(10, epoch+1, dev.g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72b9605e-5c61-433b-9006-6ffbaae6f215",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
